﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoCompile.UI
{
    public partial class EditConfig : Form
    {
        private Dictionary<String, String> allConfigNames = new Dictionary<String, String>();

        public EditConfig()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 移除配置的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(Object sender, EventArgs e)
        {
            for (Int32 i = listView1.Items.Count - 1; i >= 0; i--)
            {
                if (listView1.Items[i].Selected)
                {
                    ListViewItemEx item = listView1.Items[i] as ListViewItemEx;
                    if (item != null) allConfigNames.Remove(item.OldName);
                    listView1.Items.RemoveAt(i);
                }
            }
        }

        /// <summary>
        /// 重命名按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(Object sender, EventArgs e)
        {
            //编辑选中行
            listView1.SelectedItems[0].BeginEdit();
        }

        /// <summary>
        /// 保存修改按钮单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(Object sender, EventArgs e)
        {
            Boolean needReloadData = false;
            List<String> tempAllConfigNameList = ConfigsHelper.GetAllConfigNames();
            for (Int32 i = 0; i < tempAllConfigNameList.Count; i++)
            {
                String configName = tempAllConfigNameList[i];
                //如果包含了此键值，则证明没有被删除，否则被删除了
                if (allConfigNames.ContainsKey(configName))
                {
                    if (allConfigNames[configName] != configName) //如果键值==值，则证明没有修改过名称，否则修改了配置名称
                    {
                        ConfigsHelper.UpdateConfigName(configName, allConfigNames[configName]);
                        needReloadData = true;
                    }
                }
                else
                {
                    ConfigsHelper.RemoveConfig(configName);
                    needReloadData = true;
                }
            }

            if (needReloadData)
            {
                MessageBox.Show("保存成功", "提醒");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            this.Close();
        }

        /// <summary>
        /// 窗体加载触发的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditConfig_Load(Object sender, EventArgs e)
        {
            List<String> tempConfigNameList = ConfigsHelper.GetAllConfigNames();
            for (Int32 i = 0; i < tempConfigNameList.Count; i++)
                allConfigNames.Add(tempConfigNameList[i], tempConfigNameList[i]);
            for (Int32 i = 0; i < tempConfigNameList.Count; i++)
            {
                ListViewItemEx item = new ListViewItemEx(tempConfigNameList[i], tempConfigNameList[i]);
                listView1.Items.Add(item);
            }
        }

        /// <summary>
        /// 编辑完毕的事件（记录编辑后的值）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_AfterLabelEdit(Object sender, LabelEditEventArgs e)
        {
            ListViewItemEx item = listView1.Items[e.Item] as ListViewItemEx;
            if (item != null) allConfigNames[item.OldName] = e.Label;
        }
    }
}
